<?php
/* $Id: new_report.isets.php,v 1.6 2003/10/08 07:19:34 tuxmonkey Exp $ */
/**
 * @package Issue-Tracker
 * @subpackage Isets
 */
if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) {
  print "Direct module access forbidden.".basename(__FILE__);exit;}
  
// Make sure user is employee
if (!is_employee()) {
  access_denied();
}

$setid = $_GET['setid'];

if ($_POST['cancel']) {
   redirect("?module=isets&action=view&setid=$setid");
  }

if ($_POST['create']) {

  if (empty($_POST['duration'])) {
    push_error("Veuillez saisir la DUREE (J).");
  }

  $priv = $_POST['private'] == "on" ? "t" : "f";
  $gid = getfield ("z_issues_set","gid","setid",$setid);

  if (!errors()) {

    if (!empty($_POST['invoice'])) {
       $invoice = $_POST['invoice'] == "on" ? "t" : "f";
    } else {
       $invoice = 't';
    }

    $insert['setid']       = $setid;
    $insert['gid']         = $gid;
    $insert['duration']    = $_POST['duration'];
    $insert['opened']      = time();
    $insert['modified']    = time();
    $insert['userid']      = $_SESSION['userid'];
    $insert['units']       = compute_units($_SESSION['userid'],$insert['duration']);

    $insert['is_remote']   = $_POST['remote'];
    $insert['is_invoice']  = $invoice;

    if (!empty($_FILES['upload']['name'])) {
       $fid = upload_file('upload','s',$setid,$_FILES['upload']['name'],null,$priv);
       $insert['fid']  = $fid;
       }

    $rrid = $dbi->insert("z_set_reports",$insert,null,"z_set_reports_rrid_seq");
    unset($insert);

    $update['report']  = 't';
    $dbi->update("z_issues_set",$update,"WHERE setid='".$setid."'");
    unset($update);

    $notify_list = !is_array($_POST['notify']) ? array() : $_POST['notify'];
    set_report_notify($notify_list,$rrid);

      redirect("?module=isets&action=view&setid=$setid");
    } else {
      push_fatal_error("This report could not be created.");
    }
  }

  $title = "Rapport d'intervention pour le set #".$setid;

  $managers = group_managers();
  $smarty->assign('title',$title);
  $smarty->assign('durations',$durations);
  $smarty->assign('managers',$managers);
  $smarty->display("isets/new_report.tpl");

?>